import { createAction, actionTypeCreatorWithNameSpace } from './util';
import agent from 'agent';

const actionTypeCreator = actionTypeCreatorWithNameSpace('role');

export const listByUserId = createAction(actionTypeCreator('rolesByUserId'), (userId) => {
  return agent.get(`/api/roles/user/${userId}/all`)
    .then(response => response.body);
}, {
  fetch: {
    processing: true
  }
});

export const deleteById = createAction(actionTypeCreator('deleteById'), (id) => {
  return agent.del(`/api/roles/${id}`);
}, {
  fetch: {
    processing: true
  }
});

export const create = createAction(actionTypeCreator('create'), (role) => {
  return agent.post('/api/roles')
    .type('form')
    .send(role)
    .then(response => response.body);
}, {
  fetch: {
    processing: true
  }
});
